package http

import (
	"bytes"
	"io"
	"log"
	"net/http"

	"gitee.com/imshann/um-common/utils"
)

type Request struct {
}

func NewRequest() *Request {
	return &Request{}
}

func (r *Request) Post(url string, data map[string]interface{}) string {
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(utils.JsonEncode(data))))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	return string(body)
}
